import {
  info
} from "../../../../js/modules/info.js";
import {
  forms,
  storage,
  util
} from '../../../../js/util';
import {
  router
} from '../../../../js/router';
import authorize from '../../../../js/modules/authorize.js'
import BasePage from '../../../../js/common/base-page'
BasePage({
  pageKey:"msgList",

  /**
   * 页面的初始数据
   */
  data: {
    page: {
      PageIndex: 1,
      PageSize: 20,
      keywords: "",
      sortField: "AddTime",
      sortDirect: "desc"
    },
    list: [],
    hasMoreData: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**加载商品列表 */
  loadList() {
    var that = this;
    var page = this.data.page;
    info.loadList(page, function(content) {
      var hasMoreData = content.TotalCount > (page.PageIndex) * page.PageSize;
      var index = that.data.list.length;
      var data = content.Data;
      //当分页大于1时，累加
      if (page.PageIndex > 1) {
        data = that.data.list.concat(data);
      }
      //如果key为变量作为key值的时候要用[]包裹起来
      that.setData({
        list: data,
        hasMoreData: hasMoreData,
        isLoadComplete: true
      });
      // console.log(that.data.list);
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
    this.data.page.PageIndex = 1;
    this.loadList();
    // console.log("onPullDownRefresh");
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function(e) {
    // console.log("onReachBottom");
    if (this.data.hasMoreData) {
      var index = this.data.page.PageIndex;
      // console.log(index + 1);
      this.setData({
        "page.PageIndex":index+1
      },function(){
        this.loadList();
      })
    }
  },
  onSetReadEvent: function(e) {
    var that = this;
    info.read_all(function() {
      that.onShow();
    });
  },
  onDeleteEvent: function(e) {
    var that = this;
    util.confirm(function () {
      info.del_all(function () {
        that.onShow();
      });
    }, null, "您确定要删除所有消息吗？", "确认删除");
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.data.page.PageIndex = 1;
    this.loadList();
  },
  /*
   *跳转到页面
   * 
   */
  onSkipToPage(e) {
    var index = e.currentTarget.dataset.idx;
    var current = this.data.list[index];
    var url = "msgDetail",
      param = {
        id: current.Id
      };
    if (current.RelateType == "order" || current.RelateType == "b2border" || current.RelateType == "b2corder") {
      if (current.ReceiverFlag == 1) {
        url = "orderDetail";
        param = {
          oddNumber: current.RelateContent
        }
      }
    } else if (current.RelateType == "retreat") {
      if (current.ReceiverFlag == 1) {
        url = "retreatDetail";
        param = {
          oddNumber: current.RelateContent
        }
      }
    }
    //设为已读
    if (current.IsRead == 0) {
      info.read(current.Id, function() {});
    }
    router.navigate({
      path:url,
      params:param
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})